package com.linln.admin.SpringBoot.service.impl;

import com.linln.admin.SpringBoot.domain.StuTeam;
import com.linln.admin.SpringBoot.domain.TeamApply;
import com.linln.admin.SpringBoot.repository.StuTeamRepository;
import com.linln.admin.SpringBoot.repository.TeamApplyRepository;
import com.linln.admin.SpringBoot.service.StuTeamService;
import com.linln.admin.SpringBoot.service.TeamApplyService;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * @author Airili
 * @date 2020/12/22
 */
@Service
public class TeamApplyServiceImpl implements TeamApplyService {

    @Autowired
    private TeamApplyRepository teamApplyRepository;
    @Autowired
    private StuTeamRepository stuTeamRepository;
    @Autowired
    private StuTeamService stuTeamService;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public TeamApply getById(Long id) {
        return teamApplyRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<TeamApply> getPageList(Example<TeamApply> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return teamApplyRepository.findAll(example, page);
    }

    /**
     * 保存数据
     * @param teamApply 实体对象
     */
    @Override
    public TeamApply save(TeamApply teamApply) {
        return teamApplyRepository.save(teamApply);
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        if (statusEnum.toString().equals("OK")){
            for (Long id : idList){
                TeamApply teamApply = teamApplyRepository.findById(id).get();
                teamApply.setAble(1);

                StuTeam stuTeam = new StuTeam();
                stuTeam.setStu_id(Integer.parseInt(teamApply.getStu_id()));
                stuTeam.setTeam_id(Integer.parseInt(teamApply.getTeam_id()));
                stuTeamRepository.save(stuTeam);
            }
        }else if ("FREEZED".equals(statusEnum.getCode())){
            for (Long id :idList){

                TeamApply teamApply = teamApplyRepository.findById(id).get();
                teamApply.setAble(0);
                stuTeamService.deleteStuTeam(teamApply);

            }
        }else {
            for (Long aLong : idList) {
                teamApplyRepository.deleteById(aLong);
            }
        }
        Integer integer = teamApplyRepository.updateStatus((byte) 1, idList);
        return true;
    }

    @Override
    public List<TeamApply> list() {
        return teamApplyRepository.findAll();
    }

    @Override
    public TeamApply getByTeamId(Long id) {
        return teamApplyRepository.findByTeamId(id);
    }

    @Override
    public List<TeamApply> findByStuName(String stuId) {
        return teamApplyRepository.findByStuId(stuId);
    }


}